/**HEADER********************************************************************
* 
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2008 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: int_ctrl_mpc83xx.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*   Utilities for handling interrupts and priorities
*   Verified on MPC8313rdb, MPC8349mITXe and MPC8377rdb
*
*END************************************************************************/

#include "mqx_inc.h"
#include "bsp.h"

/* Vector Numbers are used as indexes to an array of priority masks for each vector. */
/* A mask set is 3 32-bit registers.
/* When an interrupt occurs, the mask saved for that vector is applied. */
/* The original values are saved and restored */


#define MPC83xx_MAX_IPIC_INTS        (128)

#define _GET_MIXA_GROUP_INDEX(num,index) \
     if(num < 4) { \
        index = 64 + num; \
     } \
     else if(num == 4) { \
     	index = 48; \
     } \
     else { \
        index = 12 + num; \
     } /* Endif */            

#define _GET_MIXB_GROUP_INDEX(num,index) \
     if(num < 4) { \
        index = 68 + num; \
     } \
     else { \
        index = 16 + num; \
     } /* Endif */   

      
#define _GET_SYSA_GROUP_INDEX(num,index)  index=32+num
#define _GET_SYSD_GROUP_INDEX(num,index)  index=9+num

/* Interrupt groups SYSB and SYSC are 837x, 8315 and 8308 only */

#define _GET_SYSB_GROUP_INDEX(num,index)  index=40+num
#define _GET_SYSC_GROUP_INDEX(num,index)  index=1+num


#define _GET_MIXA0_NUM(num)   num = (ipic_ptr->SMPRR_A >> 29) & 0x07
#define _GET_MIXA1_NUM(num)   num = (ipic_ptr->SMPRR_A >> 26) & 0x07
#define _GET_MIXA2_NUM(num)   num = (ipic_ptr->SMPRR_A >> 23) & 0x07
#define _GET_MIXA3_NUM(num)   num = (ipic_ptr->SMPRR_A >> 20) & 0x07
#define _GET_MIXA4_NUM(num)   num = (ipic_ptr->SMPRR_A >> 13) & 0x07
#define _GET_MIXA5_NUM(num)   num = (ipic_ptr->SMPRR_A >> 10) & 0x07
#define _GET_MIXA6_NUM(num)   num = (ipic_ptr->SMPRR_A >>  7) & 0x07
#define _GET_MIXA7_NUM(num)   num = (ipic_ptr->SMPRR_A >>  4) & 0x07

#define _GET_MIXB0_NUM(num)   num = (ipic_ptr->SMPRR_B >> 29) & 0x07
#define _GET_MIXB1_NUM(num)   num = (ipic_ptr->SMPRR_B >> 26) & 0x07
#define _GET_MIXB2_NUM(num)   num = (ipic_ptr->SMPRR_B >> 23) & 0x07
#define _GET_MIXB3_NUM(num)   num = (ipic_ptr->SMPRR_B >> 20) & 0x07
#define _GET_MIXB4_NUM(num)   num = (ipic_ptr->SMPRR_B >> 13) & 0x07
#define _GET_MIXB5_NUM(num)   num = (ipic_ptr->SMPRR_B >> 10) & 0x07
#define _GET_MIXB6_NUM(num)   num = (ipic_ptr->SMPRR_B >>  7) & 0x07
#define _GET_MIXB7_NUM(num)   num = (ipic_ptr->SMPRR_B >>  4) & 0x07

#define _GET_SYSA0_NUM(num)   num = (ipic_ptr->SIPRR_A >> 29) & 0x07
#define _GET_SYSA1_NUM(num)   num = (ipic_ptr->SIPRR_A >> 26) & 0x07
#define _GET_SYSA2_NUM(num)   num = (ipic_ptr->SIPRR_A >> 23) & 0x07
#define _GET_SYSA3_NUM(num)   num = (ipic_ptr->SIPRR_A >> 20) & 0x07
#define _GET_SYSA4_NUM(num)   num = (ipic_ptr->SIPRR_A >> 13) & 0x07
#define _GET_SYSA5_NUM(num)   num = (ipic_ptr->SIPRR_A >> 10) & 0x07
#define _GET_SYSA6_NUM(num)   num = (ipic_ptr->SIPRR_A >>  7) & 0x07
#define _GET_SYSA7_NUM(num)   num = (ipic_ptr->SIPRR_A >>  4) & 0x07

#define _GET_SYSB0_NUM(num)   num = (ipic_ptr->SIPRR_B >> 29) & 0x07
#define _GET_SYSB1_NUM(num)   num = (ipic_ptr->SIPRR_B >> 26) & 0x07
#define _GET_SYSB2_NUM(num)   num = (ipic_ptr->SIPRR_B >> 23) & 0x07
#define _GET_SYSB3_NUM(num)   num = (ipic_ptr->SIPRR_B >> 20) & 0x07
#define _GET_SYSB4_NUM(num)   num = (ipic_ptr->SIPRR_B >> 13) & 0x07
#define _GET_SYSB5_NUM(num)   num = (ipic_ptr->SIPRR_B >> 10) & 0x07
#define _GET_SYSB6_NUM(num)   num = (ipic_ptr->SIPRR_B >>  7) & 0x07
#define _GET_SYSB7_NUM(num)   num = (ipic_ptr->SIPRR_B >>  4) & 0x07

#define _GET_SYSC0_NUM(num)   num = (ipic_ptr->SIPRR_C >> 29) & 0x07
#define _GET_SYSC1_NUM(num)   num = (ipic_ptr->SIPRR_C >> 26) & 0x07
#define _GET_SYSC2_NUM(num)   num = (ipic_ptr->SIPRR_C >> 23) & 0x07
#define _GET_SYSC3_NUM(num)   num = (ipic_ptr->SIPRR_C >> 20) & 0x07
#define _GET_SYSC4_NUM(num)   num = (ipic_ptr->SIPRR_C >> 13) & 0x07
#define _GET_SYSC5_NUM(num)   num = (ipic_ptr->SIPRR_C >> 10) & 0x07
#define _GET_SYSC6_NUM(num)   num = (ipic_ptr->SIPRR_C >>  7) & 0x07
#define _GET_SYSC7_NUM(num)   num = (ipic_ptr->SIPRR_C >>  4) & 0x07

#define _GET_SYSD0_NUM(num)   num = (ipic_ptr->SIPRR_D >> 29) & 0x07
#define _GET_SYSD1_NUM(num)   num = (ipic_ptr->SIPRR_D >> 26) & 0x07
#define _GET_SYSD2_NUM(num)   num = (ipic_ptr->SIPRR_D >> 23) & 0x07
#define _GET_SYSD3_NUM(num)   num = (ipic_ptr->SIPRR_D >> 20) & 0x07
#define _GET_SYSD4_NUM(num)   num = (ipic_ptr->SIPRR_D >> 13) & 0x07
#define _GET_SYSD5_NUM(num)   num = (ipic_ptr->SIPRR_D >> 10) & 0x07
#define _GET_SYSD6_NUM(num)   num = (ipic_ptr->SIPRR_D >>  7) & 0x07
#define _GET_SYSD7_NUM(num)   num = (ipic_ptr->SIPRR_D >>  4) & 0x07

#define _SET_INT_MASK_TABLE_ENTRY(index,inum) \
   if(inum < 17) { \
      mpc83xx_int_mask_table[index].SIMSR_H |= (0x80000000 >> (15 + inum)); \
   } \
   else if(inum < 32) { \
   	  mpc83xx_int_mask_table[index].SEMSR |= (0x80000000 >> (inum - 16)); \
   } \
   else if(inum < 48) { \
   	  mpc83xx_int_mask_table[index].SIMSR_H |= (0x80000000 >> (inum - 32)); \
   } \
   else if(inum == 48) { \
   	  mpc83xx_int_mask_table[index].SEMSR |= (0x80000000); \
   } \
   else if(inum < 64) { \
   } \
   else if(inum < 96) { \
   	  mpc83xx_int_mask_table[index].SIMSR_L |= (0x80000000 >> (inum - 64)); \
   }

#define _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex) \
   mpc83xx_int_mask_table[index].SIMSR_H = mpc83xx_int_mask_table[oldindex].SIMSR_H; \
   mpc83xx_int_mask_table[index].SIMSR_L = mpc83xx_int_mask_table[oldindex].SIMSR_L; \
   mpc83xx_int_mask_table[index].SEMSR = mpc83xx_int_mask_table[oldindex].SEMSR;
        

MPC83xx_INT_MASK_STRUCT mpc83xx_int_mask_table[MPC83xx_MAX_IPIC_INTS];


/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_ipic_set_group_prios_internal
* Returned Value   : none
* Comments         :
*    Sets relative priorities in a group and updates mask table
*
*END*------------------------------------------------------------------------*/
static void _mpc83xx_ipic_set_group_prios_internal
   (
      /* [IN] pointer to Interrupt group parameter structure */
      pointer param
      
   )
{ /* Body */
   MPC83xx_INT_GROUP_PARAM_STRUCT_PTR   param_ptr = param;
   MPC83xx_IPIC_STRUCT_PTR              ipic_ptr;
   uint_32                              immr;

   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);
   
   
   switch (param_ptr->GID) {
   	
      case MPC83xx_IPIC_INT_GID_SYSA :
         ipic_ptr->SIPRR_A = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_IPSA_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_IPSA_SPREAD;
         }
         break;

      case MPC83xx_IPIC_INT_GID_SYSB :
         ipic_ptr->SIPRR_B = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_IPSB_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_IPSB_SPREAD;
         }
         break;

      case MPC83xx_IPIC_INT_GID_SYSC :
         ipic_ptr->SIPRR_C = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_IPSC_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_IPSC_SPREAD;
         }
         break;

      case MPC83xx_IPIC_INT_GID_SYSD :
         ipic_ptr->SIPRR_D = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_IPSD_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_IPSD_SPREAD;
         }
         break;

      case MPC83xx_IPIC_INT_GID_MIXA :
         ipic_ptr->SMPRR_A = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_MPSA_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_MPSA_SPREAD;
         }
         break;

      case MPC83xx_IPIC_INT_GID_MIXB :
         ipic_ptr->SMPRR_B = (param_ptr->PRIO_LIST[0] << 29) |
                             (param_ptr->PRIO_LIST[1] << 26) |
                             (param_ptr->PRIO_LIST[2] << 23) |
                             (param_ptr->PRIO_LIST[3] << 20) |
                             (param_ptr->PRIO_LIST[4] << 13) |
                             (param_ptr->PRIO_LIST[5] << 10) |
                             (param_ptr->PRIO_LIST[6] <<  7) |
                             (param_ptr->PRIO_LIST[7] <<  4); 
         if(param_ptr->GTYPE == MPC83xx_IPIC_INT_GTYPE_GROUP) {
         	ipic_ptr->SICFR &= ~MPC83xx_IPIC_SICFR_MPSB_SPREAD;
         }
         else {
         	ipic_ptr->SICFR |= MPC83xx_IPIC_SICFR_MPSB_SPREAD;
         }
         break;
         
   } /* Endswitch */

   _int_enable();
} /* Endbody */

/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_update_int_mask_table
* Returned Value   : none
* Comments         :
*    Updates interrupt mask table. This table is going to be used by 
*    mpc83xx_external_isr to handle nested interrupts
*
*END*------------------------------------------------------------------------*/
static void _mpc83xx_update_int_mask_table
   (
      void
   )
{ /* Body */
   MPC83xx_IPIC_STRUCT_PTR              ipic_ptr;
   uint_32                              immr;
   uint_32                              sicfg;
   uint_32                              num, index, oldindex;
   uchar                                sysaspread = 0, sysbspread = 0, syscspread = 0, sysdspread = 0;
   uchar                                mixaspread = 0, mixbspread = 0;

   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);
   
   sicfg = ipic_ptr->SICFR;
   
   /* Get the Priority scheme */
   sysaspread = (sicfg & MPC83xx_IPIC_SICFR_IPSA_SPREAD) ? 1 : 0;
   sysbspread = (sicfg & MPC83xx_IPIC_SICFR_IPSB_SPREAD) ? 1 : 0;
   syscspread = (sicfg & MPC83xx_IPIC_SICFR_IPSC_SPREAD) ? 1 : 0;
   sysdspread = (sicfg & MPC83xx_IPIC_SICFR_IPSD_SPREAD) ? 1 : 0;
   mixaspread = (sicfg & MPC83xx_IPIC_SICFR_MPSA_SPREAD) ? 1 : 0;
   mixbspread = (sicfg & MPC83xx_IPIC_SICFR_MPSB_SPREAD) ? 1 : 0;

   _mem_zero(mpc83xx_int_mask_table, sizeof(mpc83xx_int_mask_table));

   /* Interrupts masks are created in priority order */
   /* The order depends on each groups Priority scheme */
   /* It is dynamic, so the masks may need to be rebuilt often */

   /* Except for the user specified Highest Priority Int, the source at MIXA0 is the highest priority int */
   /* Who is number 2 depends on the combination of group priority schemes. */
   
   /* This code determines the priority order and builds a masking table. */
   /* For each priority level it determines the interrupt source and vector (index) */
   /* and copies the previous 3 register mask and appends the current interrupt source's */
   /* mask bit to its mask */
   
    _GET_MIXA0_NUM(num);                       /* find our source */
    _GET_MIXA_GROUP_INDEX(num,index);          /* establish our vector */          

   /* Find the next highest level and so on */
   
   if(!mixaspread) {
      /* MIXA1 */     
      oldindex = index;
   	  _GET_MIXA1_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);         
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);     /* No need to copy the mask is 0, but if order changes */     
     _SET_INT_MASK_TABLE_ENTRY(index,oldindex);  
      
      /* MIXA2 */
      oldindex = index;
      _GET_MIXA2_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* MIXA3 */
      oldindex = index;
      _GET_MIXA3_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   if(mixbspread) {
   /* MIXB0 Spread */
      oldindex = index;                            
      _GET_MIXB0_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x ||  PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
  if(!sysbspread) {
      /* SYSB0 grouped */
      oldindex = index;
      _GET_SYSB0_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB1 grouped */
      oldindex = index;
      _GET_SYSB1_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB2 grouped */
      oldindex = index;
      _GET_SYSB2_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB3 grouped */
      oldindex = index;
      _GET_SYSB3_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
    } /* Endif */
#endif

   if(mixaspread) {
     /* MIXA1 spread */
      oldindex = index;
      _GET_MIXA1_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
 
#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(!sysbspread) {
      /* SYSB4 grouped */
      oldindex = index;
      _GET_SYSB4_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB5 grouped */
      oldindex = index;
      _GET_SYSB5_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB6 grouped */
      oldindex = index;
      _GET_SYSB6_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSB7 grouped */
      oldindex = index;
      _GET_SYSB7_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
    } /* Endif */  
#endif
   
   if(mixbspread) {
   	  /* MIXB1 spread */
	  oldindex = index;
      _GET_MIXB1_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   }
   else {
   	  /* MIXB0 grouped */
      oldindex = index;
      _GET_MIXB0_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
         	  
   	  /* MIXB1 grouped */
      oldindex = index;
      _GET_MIXB1_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
   	  /* MIXB2 grouped */
      oldindex = index;
      _GET_MIXB2_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
   	  /* MIXB3 grouped */
      oldindex = index;
      _GET_MIXB3_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   if(!sysaspread) {
      /* SYSA0 grouped */
      oldindex = index;
      _GET_SYSA0_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA1 grouped */
      oldindex = index;
      _GET_SYSA1_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA2 grouped */
      oldindex = index;
      _GET_SYSA2_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA3 grouped */
      oldindex = index;
      _GET_SYSA3_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   /* MIXA2 spread */
   if(mixaspread) {
      oldindex = index;
      _GET_MIXA2_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   if(!sysaspread) {
      /* SYSA4 grouped */
      oldindex = index;
      _GET_SYSA4_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA5 grouped */
      oldindex = index;
      _GET_SYSA5_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA6 grouped */
      oldindex = index;
      _GET_SYSA6_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSA7 grouped */
      oldindex = index;
      _GET_SYSA7_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   if(!mixaspread) {
      /* MIXA4 grouped */
      oldindex = index;
      _GET_MIXA4_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* MIXA5 grouped */
      oldindex = index;
      _GET_MIXA5_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* MIXA6 grouped */
      oldindex = index;
      _GET_MIXA6_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* MIXA5 grouped */
      oldindex = index;
      _GET_MIXA7_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   	
   /* MIXB2 spread */
   if(mixbspread) {
      oldindex = index;
      _GET_MIXB2_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x ||  PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
    if(!syscspread) {
      /* SYSC0 grouped */
      oldindex = index;
      _GET_SYSC0_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC1 grouped */
      oldindex = index;
      _GET_SYSC1_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC2 grouped */
      oldindex = index;
      _GET_SYSC2_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC3 grouped */
      oldindex = index;
      _GET_SYSC3_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
    } /* Endif */
#endif

   /* MIXA3 spread */
   if(mixaspread) {
      oldindex = index;
      _GET_MIXA3_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);   	  
   } /* Endif */
   
#if PSP_MQX_CPU_IS_MPC837x ||  PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315) 
   if(!syscspread) {
      /* SYSC4 grouped */
      oldindex = index;
      _GET_SYSC4_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC5 grouped */
      oldindex = index;
      _GET_SYSC5_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC6 grouped */
      oldindex = index;
      _GET_SYSC6_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSC7 grouped */
      oldindex = index;
      _GET_SYSC7_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
    } /* Endif */
#endif
   
   if(mixbspread) {
      /* MIXB3 spread */
      oldindex = index;
      _GET_MIXB3_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   }
   else {
   	  /* MIXB4 grouped */
      oldindex = index;
      _GET_MIXB4_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
         	  
   	  /* MIXB5 grouped */
      oldindex = index;
      _GET_MIXB5_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
   	  /* MIXB6 grouped */
      oldindex = index;
      _GET_MIXB6_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
   	  /* MIXB7 grouped */
      oldindex = index;
      _GET_MIXB7_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   if(!sysdspread) {
      /* SYSD0 grouped */
      oldindex = index;
      _GET_SYSD0_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD1 grouped */
      oldindex = index;
      _GET_SYSD1_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD2 grouped */
      oldindex = index;
      _GET_SYSD2_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD3 grouped */
      oldindex = index;
      _GET_SYSD3_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   if(mixaspread)
   {
      /* MIXA4 spread */
      oldindex = index;
      _GET_MIXA4_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   if(!sysdspread) {
      /* SYSD4 grouped */
      oldindex = index;
      _GET_SYSD4_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD5 grouped */
      oldindex = index;
      _GET_SYSD5_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD6 grouped */
      oldindex = index;
      _GET_SYSD6_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
      
      /* SYSD7 grouped */
      oldindex = index;
      _GET_SYSD7_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   if(mixbspread)
   {
      /* MIXB4 spread */
      oldindex = index;
      _GET_MIXB4_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
#if MQX_CPU != PSP_CPU_MPC8308
   /* GTM4  No GTM 4-7 on 8308 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM4;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

#if PSP_MQX_CPU_IS_MPC837x ||  PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB0 spread */
      oldindex = index;
      _GET_SYSB0_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA0 spread */
      oldindex = index;
      _GET_SYSA0_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if !PSP_MQX_CPU_IS_MPC830x
   /* GTM8 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM8;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(syscspread)
   {
      /* SYSC0 spread */
      oldindex = index;
      _GET_SYSC0_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysdspread)
   {
      /* SYSD0 spread */
      oldindex = index;
      _GET_SYSD0_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   /* on 832x I/O pins are QUICC Engine Ports others use GPIO */
   /* but they share the same index and priority */
   oldindex = index;
   index = MPC83xx_INT_ID_GPIO;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(mixaspread)
   {
      /* MIXA5 spread */
      oldindex = index;
      _GET_MIXA5_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if MQX_CPU == PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC834x || (MQX_CPU == PSP_CPU_MPC8309) || (MQX_CPU == PSP_CPU_MPC8306)
   /* GPIO2 */
   oldindex = index;
   index = MPC83xx_INT_ID_GPIO2;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif
   
#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB1 spread */
      oldindex = index;
      _GET_SYSB1_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA1 spread */
      oldindex = index;
      _GET_SYSA1_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   /* DDR */
   oldindex = index;
   index = MPC83xx_INT_ID_DDR;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(syscspread)
   {
      /* SYSC1 spread */
      oldindex = index;
      _GET_SYSC1_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif
   
   if(sysdspread)
   {
      /* SYSD1 spread */
      oldindex = index;
      _GET_SYSD1_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
   /* LBC */
   oldindex = index;
   index = MPC83xx_INT_ID_LBC;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(mixbspread)
   {
      /* MIXB5 spread */
      oldindex = index;
      _GET_MIXB5_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   /* GTM2 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM2;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB2 spread */
      oldindex = index;
      _GET_SYSB2_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA2 spread */
      oldindex = index;
      _GET_SYSA2_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if  !PSP_MQX_CPU_IS_MPC830x 
   /* GTM6 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM6;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(syscspread)
   {
      /* SYSC2 spread */
      oldindex = index;
      _GET_SYSC2_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysdspread)
   {
      /* SYSD2 spread */
      oldindex = index;
      _GET_SYSD2_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if  !PSP_MQX_CPU_IS_MPC830x 
   /* PMC - No PMC interrupt on 8308 */
   oldindex = index;
   index = MPC83xx_INT_ID_PMC;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

   if(mixaspread)
   {
      /* MIXA6 spread */
      oldindex = index;
      _GET_MIXA6_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
   /* MSIR2 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR2;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(sysbspread)
   {
      /* SYSB3 spread */
      oldindex = index;
      _GET_SYSB3_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA3 spread */
      oldindex = index;
      _GET_SYSA3_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
   /* MSIR3 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR3;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(syscspread)
   {
      /* SYSC3 spread */
      oldindex = index;
      _GET_SYSC3_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysdspread)
   {
      /* SYSD3 spread */
      oldindex = index;
      _GET_SYSD3_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if MQX_CPU == PSP_CPU_MPC8315
  /* TDM Error */
  oldindex = index;
  index = MPC83xx_INT_ID_TDM_ERR;
  _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
  _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

   if(mixbspread)
   {
      /* MIXB6 spread */
      oldindex = index;
      _GET_MIXB6_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   /* GTM3 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM3;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB4 spread */
      oldindex = index;
      _GET_SYSB4_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA4 spread */
      oldindex = index;
      _GET_SYSA4_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if  !PSP_MQX_CPU_IS_MPC830x 
   /* GTM7 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM7;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(syscspread)
   {
      /* SYSC4 spread */
      oldindex = index;
      _GET_SYSC4_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

    if(sysdspread)
   {
      /* SYSD4 spread */
      oldindex = index;
      _GET_SYSD4_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
   /* MSIR4 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR4;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

   if(mixaspread)
   {
      /* MIXA7 spread */
      oldindex = index;
      _GET_MIXA7_NUM(num);
      _GET_MIXA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
   /* MSIR5 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR5;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(sysbspread)
   {
      /* SYSB5 spread */
      oldindex = index;
      _GET_SYSB5_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA5 spread */
      oldindex = index;
      _GET_SYSA5_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
   /* MSIR6 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR6;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

   if(syscspread)
   {
      /* SYSC5 spread */
      oldindex = index;
      _GET_SYSC5_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysdspread)
   {
      /* SYSD5 spread */
      oldindex = index;
      _GET_SYSD5_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC837x || (MQX_CPU == PSP_CPU_MPC8315) || (MQX_CPU == PSP_CPU_MPC8308)
    /* MSIR7 */
   oldindex = index;
   index = MPC837x_INT_ID_MSIR7;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

   if(mixbspread)
   {
      /* MIXB7 spread */
      oldindex = index;
      _GET_MIXB7_NUM(num);
      _GET_MIXB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

   /* GTM1 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM1;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB6 spread */
      oldindex = index;
      _GET_SYSB6_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysaspread)
   {
      /* SYSA6 spread */
      oldindex = index;
      _GET_SYSA6_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if  !PSP_MQX_CPU_IS_MPC830x 
   /* GTM5 */
   oldindex = index;
   index = MPC83xx_INT_ID_GTM5;
   _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
   _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif
   
#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(syscspread)
   {
      /* SYSC6 spread */
      oldindex = index;
      _GET_SYSC6_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif
   
    if(sysdspread)
   {
      /* SYSD6 spread */
      oldindex = index;
      _GET_SYSD6_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
   if(sysbspread)
   {
      /* SYSB7 spread */
      oldindex = index;
      _GET_SYSB7_NUM(num);
      _GET_SYSB_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

    if(sysaspread)
   {
      /* SYSA7 spread */
      oldindex = index;
      _GET_SYSA7_NUM(num);
      _GET_SYSA_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */

#if PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
  /* DMAC Error */
  oldindex = index;
  index = MPC83xx_INT_ID_DMAC_ERR;
  _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
  _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
#endif

#if PSP_MQX_CPU_IS_MPC837x || PSP_MQX_CPU_IS_MPC830x || (MQX_CPU == PSP_CPU_MPC8315)
    if(syscspread)
   {
      /* SYSC7 spread */
      oldindex = index;
      _GET_SYSC7_NUM(num);
      _GET_SYSC_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
#endif

   if(sysdspread)
   {
      /* SYSD7 spread */
      oldindex = index;
      _GET_SYSD7_NUM(num);
      _GET_SYSD_GROUP_INDEX(num,index);
      _COPY_OLD_INT_MASK_TABLE_ENTRY(index,oldindex);          
      _SET_INT_MASK_TABLE_ENTRY(index,oldindex);
   } /* Endif */
   
} /* Endbody */


/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_ipic_init
* Returned Value   : none
* Comments         :
*    Initializes MPC83xx Integrated Programmable Interrupt Controller
*
*END*------------------------------------------------------------------------*/
void _mpc83xx_ipic_init
   (
      void
   )
{ /* Body */
   MPC83xx_IPIC_STRUCT_PTR        ipic_ptr;
   uint_32                        immr;
   
   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);
   
   ipic_ptr->SIMSR_H = 0;
   ipic_ptr->SIMSR_L = 0;
   ipic_ptr->SEMSR = 0;
   ipic_ptr->SERSR = 0xFFFFFFFF;  /* Clear Error status register */

   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_sysa_param_list);
   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_sysb_param_list);
   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_sysc_param_list);
   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_sysd_param_list);
   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_mixa_param_list);
   _mpc83xx_ipic_set_group_prios_internal(&mpc83xx_int_group_mixb_param_list);
   _mpc83xx_update_int_mask_table();   

} /* Endbody */


/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_ipic_set_group_prios
* Returned Value   : none
* Comments         :
*   Dynamically Sets relative priorities in a group and updates mask table
*
*END*------------------------------------------------------------------------*/
void _mpc83xx_ipic_set_group_prios
   (
      /* [IN] pointer to Interrupt group parameter structure */
      pointer param
   )
{ /* Body */

   _int_disable();   
   
   _mpc83xx_ipic_set_group_prios_internal(param);
   _mpc83xx_update_int_mask_table();
   
   _int_enable();   
   
} /* Endbody */



/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_enable_interrupt
* Returned Value   : none
* Comments         :
*    Enables an interrupt at IPIC level
*
*END*------------------------------------------------------------------------*/

void _mpc83xx_enable_interrupt
   (
      /* [IN] the interrupt vector number */
      uint_32 vector
   )
{ /* Body */
   MPC83xx_IPIC_STRUCT_PTR   ipic_ptr;
   uint_32                   immr;
   uchar                     inum;                   

   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);
   
   inum = (uchar)(vector - MPC83xx_IRQ_BASE_VECTOR);
   
   if(inum < 17) {
      ipic_ptr->SIMSR_H |= (0x80000000 >> (15 + inum));
   }
   else if(inum < 32) {
   	  ipic_ptr->SEMSR |= (0x80000000 >> (inum - 16));
   }
   else if(inum < 48) {
   	  ipic_ptr->SIMSR_H |= (0x80000000 >> (inum - 32));
   }
   else if(inum == 48) {
   	  ipic_ptr->SEMSR |= (0x80000000);
   }
   else if(inum < 64) {
   	// Do nothing
   }
   else if(inum < 96) {
   	  ipic_ptr->SIMSR_L |= (0x80000000 >> (inum - 64));
   }

} /* Endbody */


/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : _mpc83xx_disable_external_interrupt
* Returned Value   : none
* Comments         :
*    Disables an interrupt at IPIC level
*
*END*------------------------------------------------------------------------*/

void _mpc83xx_disable_interrupt
   (
      /* [IN] the interrupt vector number */
      uint_32 vector

   )
{ /* Body */
   MPC83xx_IPIC_STRUCT_PTR   ipic_ptr;
   uint_32                   immr;
   uchar                     inum;                   

   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);
   
   inum = (uchar)(vector - MPC83xx_IRQ_BASE_VECTOR);
   
   if(inum < 17) {
      ipic_ptr->SIMSR_H &= ~(0x80000000 >> (15 + inum));
   }
   else if(inum < 32) {
   	  ipic_ptr->SEMSR &= ~(0x80000000 >> (inum - 16));
   }
   else if(inum < 48) {
   	  ipic_ptr->SIMSR_H &= ~(0x80000000 >> (inum - 32));
   }
   else if(inum == 48) {
   	  ipic_ptr->SEMSR &= ~(0x80000000);
   }
   else if(inum < 64) {
   	// Do nothing
   }
   else if(inum < 96) {
   	  ipic_ptr->SIMSR_L &= ~(0x80000000 >> (inum - 64));
   }

} /* Endbody */


/*ISR*---------------------------------------------------------------------
*
* Function Name    : _mpc83xx_external_isr
* Comments         :
*   This ISR is to be installed into the external interrupt vector
* location in order to capture all external interrupts
*
*END*----------------------------------------------------------------------*/

void _mpc83xx_external_isr
   (
      pointer dummy
   )
{ /* Body */
   MPC83xx_IPIC_STRUCT_PTR   ipic_ptr;
   uint_32                   sivec_int_code, vector;
   uint_32                   simsr_h, simsr_l,semsr;
   uint_32                   immr;

   immr = (uint_32)BSP_IMMR_BASE;
   
   ipic_ptr = (MPC83xx_IPIC_STRUCT_PTR)(immr + MPC83xx_IPIC_BASE);

   /* Get the interrupt VECTOR offset */
   sivec_int_code = ipic_ptr->SIVCR & MPC83xx_IPIC_SIVCR_IVEC_MASK;
   
   vector = sivec_int_code + MPC83xx_IRQ_BASE_VECTOR;

   /* save the original interrupt masks */      
   simsr_h = ipic_ptr->SIMSR_H;
   simsr_l = ipic_ptr->SIMSR_L;
   semsr = ipic_ptr->SEMSR;

   ipic_ptr->SIMSR_H = simsr_h & mpc83xx_int_mask_table[sivec_int_code].SIMSR_H;
   ipic_ptr->SIMSR_L = simsr_l & mpc83xx_int_mask_table[sivec_int_code].SIMSR_L;
   ipic_ptr->SEMSR = semsr & mpc83xx_int_mask_table[sivec_int_code].SEMSR;

   /* dummy read needed to avert hardware race */
   ipic_ptr->SIMSR_H;
   
   /* allow the core to service interrupts */
   _PSP_SET_SR (_PSP_GET_SR() | PSP_MSR_EE);

   /* invoke the interrupt service routine associated with the interrupt code */
   _PSP_GENERATE_INTERRUPT(vector);

   /* prevent the core from servicing interrupts */
   _PSP_SET_SR (_PSP_GET_SR() & ~PSP_MSR_EE);

   /* restore the original interrupt masks */
   ipic_ptr->SIMSR_H = simsr_h;
   ipic_ptr->SIMSR_L = simsr_l;
   ipic_ptr->SEMSR = semsr;

} /* Endbody */


/* EOF */
